package com.数组;

/**
 * 给定一个数组，将数组中的元素向右移动 k 个位置，其中 k 是非负数。
 *
 * 示例 1:
 *
 * 输入: [1,2,3,4,5,6,7] 和 k = 3
 * 输出: [5,6,7,1,2,3,4]
 * 解释:
 * 向右旋转 1 步: [7,1,2,3,4,5,6]
 * 向右旋转 2 步: [6,7,1,2,3,4,5]
 * 向右旋转 3 步: [5,6,7,1,2,3,4]
 */
public class 旋转数组 {
    static  class Solution {
        /**
         * 暴力法，将每个数旋转K次
         * @param nums
         * @param k
         */
        public void rotate(int[] nums, int k) {
          for(int i = 0;i<k;i++){
              int pre = nums[nums.length-1];
              for(int j = 0;j<nums.length;j++){
                  int tmp = nums[j];
                  nums[j] = pre;
                  pre = tmp;
              }
          }
        }
        public void rotate2(int[] nums, int k) {
            int temp, previous;
            for (int i = 0; i < k; i++) {
                previous = nums[nums.length - 1];
                for (int j = 0; j < nums.length; j++) {
                    temp = nums[j];
                    nums[j] = previous;
                    previous = temp;
                }
            }
        }









        public static void main(String[] args) {
            Solution solution = new Solution();
            int[] nums = {1,2,3,4,5};
            solution.rotate2(nums,3);
        }

}}
